package com.jdd.modules.operationLog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.api.vo.Result;
import com.jdd.common.constant.CommonConstant;
import com.jdd.common.constant.JddConstant;
import com.jdd.common.system.util.JwtUtil;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.CacheUtil;
import com.jdd.common.util.MathUtil;
import com.jdd.common.util.PasswordUtil;
import com.jdd.common.util.encryption.AesEncryptUtil;
import com.jdd.common.util.encryption.RASUtil;
import com.jdd.modules.configcartype.entity.ConfigCarType;
import com.jdd.modules.configcartype.service.IConfigCarTypeService;
import com.jdd.modules.operationLog.entity.ParkHandOverLog;
import com.jdd.modules.operationLog.mapper.ParkHandOverLogMapper;
import com.jdd.modules.operationLog.service.IParkHandOverLogService;
import com.jdd.modules.order.entity.ParkOrderInfo;
import com.jdd.modules.parking.entity.ParkArmInfo;
import com.jdd.modules.parking.entity.ParkCarOutLog;
import com.jdd.modules.parking.entity.ParkInfo;
import com.jdd.modules.parking.entity.ParkWorkStation;
import com.jdd.modules.parking.mapper.ParkWorkStationMapper;
import com.jdd.modules.parking.service.IParkArmInfoService;
import com.jdd.modules.parking.service.IParkInfoService;
import com.jdd.modules.parking.service.IParkWorkStationService;
import com.jdd.modules.sdkidentifyresultpushlog.mapper.SdkIdentifyResultPushLogMapper;
import com.jdd.modules.system.entity.SysUser;
import com.jdd.modules.system.model.HandOverModelVo;
import com.jdd.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.jdd.modules.system.controller.LoginController.RESULT500;

/**
 * @Description: 车场交接记录
 * @Author: jdd
 * @Date:   2021-11-04
 * @Version: V1.0
 */
@Service
@Slf4j
public class ParkHandOverLogServiceImpl extends ServiceImpl<ParkHandOverLogMapper, ParkHandOverLog> implements IParkHandOverLogService {
    @Resource
    private IParkWorkStationService parkWorkStationService;
    @Resource
    private IParkArmInfoService parkArmInfoService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IParkInfoService parkInfoService;
    @Resource
    private IConfigCarTypeService carTypeService;
    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private ParkHandOverLogMapper parkHandOverLogMapper;
    @Resource
    private ParkWorkStationMapper parkWorkStationMapper;

    @Override
    public Map<String, Object> getHandOverData(String userId) {
        Map<String, Object> map = new HashMap<>();
        //工作站名称
        //工作站操作员名
        //工作站上班时间
        QueryWrapper<ParkWorkStation> wrapper = new QueryWrapper<>();
        wrapper.eq("used_user_id", userId);
        wrapper.orderByDesc("up_time");
        wrapper.last(" limit 1");
        ParkWorkStation workStation = parkWorkStationMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(workStation)) {
            map.put("id", workStation.getId());
            map.put("workName", workStation.getWorkName());
            map.put("operationName", workStation.getRealName());
//            map.put("upTime",workStation.getUpTime());
            //人工收费4个
            //电子支付4个
            ParkHandOverLog hand = parkHandOverLogMapper.getHand(workStation.getOperationName());
            if (ObjectUtil.isEmpty(hand)) {
                log.error("---------------getHandOverData提示没找到交接班-------------");
                return null;
            }
            map.put("upTime", hand.getUpTime());
            map.put("out_num", hand.getAllOutNum());
            map.put("free_num", hand.getFreeOut());
            map.put("normal_fee", hand.getNormalFeeOut());
            map.put("normal_no_fee", hand.getNormalNoFeeOut());
            map.put("abnormal_num", hand.getAbnormalOut());
            map.put("manual_receivable", hand.getManualReceivable());
            map.put("manual_paid", hand.getManualPaid());
            map.put("manual_free", hand.getManualFree());
            map.put("manual_deduction", BigDecimal.ZERO);
            map.put("online_receivable", hand.getOnlineReceivable());
            map.put("online_paid", hand.getOnlinePaid());
            map.put("online_free", hand.getOnlineFree());
            map.put("online_deduction", BigDecimal.ZERO);
        }
        return map;
    }

    @Override
    public Result<JSONObject> handOver(HandOverModelVo handOverModelVo) throws Exception {
        Result<JSONObject> result = new Result<>();
        //接班人登录
        String username = handOverModelVo.getUsername();
        String oldpassword = handOverModelVo.getPassword().trim();
        String password = AesEncryptUtil.desEncrypt(URLDecoder.decode(oldpassword, CommonConstant.DEFAULT_CHARSET).replace(' ', '+'));
        if (StringUtils.isEmpty(password)) {
            password = oldpassword;
        }
        SysUser sysUser = sysUserService.getUserByName(username);
        result = sysUserService.checkUserIsEffective(sysUser);
        if (RESULT500 == result.getCode()) {
            return result;
        }
        // 校验用户是否有效
        if (!result.isSuccess()) {
            return result;
        }
        //校验数据的合法性
        if (StringUtils.isEmpty(sysUser.getSignature())) {
            result.error500("该用户非法");
            return result;
        }
        try {
            if (!RASUtil.verifySign(sysUser.getSignature(), sysUser.getUsername())) {
                result.error500("该用户非法");
                return result;
            }
        } catch (Exception e) {
            log.error("用户非法登录---> {}", e.getMessage());
            result.error500("该用户非法");
            return result;
        }
        //3. 校验用户名或密码是否正确
        String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
        String syspassword = sysUser.getPassword();
        if (!syspassword.equals(userpassword)) {
            result.error500("用户名或密码错误");
            return result;
        }
        //4. 校验是不是没在工作站登录的人
        //为true则代表已在其他工作站登录
        if (checkRoleAndRepetLogin(username)) {
            result.error500("当前接班人已有其他工作站！");
            return result;
        }
        //拿到当前登录人的账户名
        Subject subject = SecurityUtils.getSubject();
        LoginUser nowUser = (LoginUser) subject.getPrincipal();
        String logoutName = nowUser.getUsername();
        //更新工作站
        ParkWorkStation workStation = changeWorkStation(logoutName, sysUser);
        if (ObjectUtil.isEmpty(workStation)) {
            log.error("--------------handOver提示工作站更新失败----------------------");
            throw new Exception("工作站更新失败");
        }
        //更新车道
        if (!parkArmInfoService.updateArmByWork(workStation.getId(), sysUser.getId())) {
            log.error("--------------handOver提示更新车道失败----------------------");
            throw new Exception("更新车道失败");
        }
        // 更新交接班
        if (!changeHandOver(logoutName, username, handOverModelVo.getRemark())) {
            log.error("--------------handOver提示更新交接班失败----------------------");
            throw new Exception("交接班更新失败");
        }
        //通过used_user_id地址生成一个新的交班记录
        if (!createHandOverLog(workStation.getId(), handOverModelVo.getRemark())) {
            log.error("--------------handOver提示交接班生成失败----------------------");
            throw new Exception("交接班记录生成失败");
        }
        //数据完成没问题后再退出，然后登录新的
        //退出
        logout();
        //登录新的
        userInfo(sysUser, result);
        return result;
    }

    /*** 功能描述: 换班时创建个新的交接班记录
     * @param stationId 工作站id
     * @Author: lcy
     * @Date: 2022/1/10
     */
    public boolean createHandOverLog(String stationId, String remark) {
        //生成一条交接班记录表
        ParkHandOverLog parkHandOverLog = new ParkHandOverLog();
        QueryWrapper<ParkArmInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_station_id", stationId);
        //当前stationId地址对应的车道
        List<ParkArmInfo> armInfos = parkArmInfoService.list(queryWrapper);
        if (ObjectUtil.isEmpty(armInfos)) {
            log.error("--------------createHandOverLog提示查询车道失败----------------------");
            return false;
        }
        //当前车道对应的工作站
        ParkWorkStation workStation = parkWorkStationMapper.selectById(armInfos.get(0).getWorkStationId());
        if (ObjectUtil.isEmpty(workStation)) {
            log.error("--------------createHandOverLog提示查询工作站失败----------------------");
            return false;
        }
        //交接班记录拷贝属性从工作站
        BeanUtils.copyProperties(workStation, parkHandOverLog);
        parkHandOverLog.setRemark(remark);
        parkHandOverLog.setId(null);
        parkHandOverLog.setUpTime(new Date());
        parkHandOverLog.setCreateTime(new Date());
        parkHandOverLog.setOperationName(workStation.getOperationName());
        parkHandOverLog.setRealName(workStation.getRealName());
        parkHandOverLog.setManualPaid(BigDecimal.ZERO);
        parkHandOverLog.setManualReceivable(BigDecimal.ZERO);
        parkHandOverLog.setManualDeduction(BigDecimal.ZERO);
        parkHandOverLog.setManualFree(BigDecimal.ZERO);
        parkHandOverLog.setOnlinePaid(BigDecimal.ZERO);
        parkHandOverLog.setOnlineDeduction(BigDecimal.ZERO);
        parkHandOverLog.setOnlineReceivable(BigDecimal.ZERO);
        parkHandOverLog.setOnlineFree(BigDecimal.ZERO);
        parkHandOverLog.setAbnormalOut(0);
        parkHandOverLog.setFreeOut(0);
        parkHandOverLog.setNormalNoFeeOut(0);
        parkHandOverLog.setNormalFeeOut(0);
        parkHandOverLog.setAllOutNum(0);
        return parkHandOverLogMapper.insert(parkHandOverLog) > 0;
    }

    /*** 功能描述: 检查接班人的合理性,返回true则表示不能登录
     * @param username 账号
     * @Author: lcy
     * @Date: 2021/11/27
     */
    public boolean checkRoleAndRepetLogin(String username) {
        //所有岗亭和管理的账户
        List<SysUser> gangTings = sysUserService.getGangTing();
        if (ObjectUtil.isEmpty(gangTings)) {
            log.error("-----------岗亭人员为空！-------------");
            return true;
        }
        //只拿到账号名
        List<String> gangtings = gangTings.stream().map(SysUser::getUsername).collect(Collectors.toList());
        //当前登录人是否岗亭和管理的账户
        if (!gangtings.contains(username)) {
            log.error("----------当前换班人员非岗亭和管理的账户---------");
            return true;
        }
        //查看当前用户名是否在其他工作站也登陆了
        QueryWrapper<ParkWorkStation> wrapper = new QueryWrapper<>();
        wrapper.eq("operation_name", username);
        wrapper.eq("status", "1");
        List<ParkWorkStation> list = parkWorkStationService.list(wrapper);
        if (ObjectUtil.isNotEmpty(list) && list.size() > 0) {
            log.error("----------接班人员已经有其他工作站！-----------");
            return true;
        }
        return false;
    }

    @Override
    /*** 功能描述: 传入parkCarOutLog代表当前车辆出场操作，可以更新总出场数，异常数，正常(免费)放行数
     *            传入parkOrderInfo代表当前车辆产生了费用，可以判断钱数和是否免费
     *            parkCarOutLog无法判断是否免费，是否免费需要用parkOrderInfo的优惠卷和实付金额判断
     *            传入workUser代表当前岗亭人员
     *            传入abnormal代表异常标识，目前出场纪录没有异常字段，所以需要一个异常标识
     * @param parkCarOutLog 出场纪录
     * @param parkOrderInfo 订单信息
     * @param workUser 岗亭人员
     * @param abnormal 异常数
     * @Author: lcy
     * @Date: 2021/12/6
     */
    public  ParkHandOverLog updateHandOverAuto(ParkCarOutLog parkCarOutLog, ParkOrderInfo parkOrderInfo, String workUser, int abnormal) {
        List<ParkWorkStation> stations = parkWorkStationMapper.getWorkStation(workUser);
        if (ObjectUtil.isEmpty(stations)) {
            log.error("-----------更新交接班时未找到工作站！！！--------");
            return null;
        }
        ParkHandOverLog hand = parkHandOverLogMapper.getHand(workUser);
        if (ObjectUtil.isEmpty(hand)) {
            log.error("----------------交接班数据统计异常，未找到交接班记录！！！------------");
            return null;
        }
        //出场数量
        if (ObjectUtil.isNotEmpty(parkCarOutLog)) {
            //总数
            hand.setAllOutNum(hand.getAllOutNum() + 1);
            //异常1则正常0，异常0则正常1
            if (JddConstant.IntNumber.INT_ONE == abnormal) {
                hand.setAbnormalOut(hand.getAbnormalOut() + 1);
            } else {
                //临时车看是否付费
                if (JddConstant.MqttRecResultPushLog.TYPE_ZERO.equals(parkCarOutLog.getCarNature())) {
                    //针对监控页面和云端下发的情况，这两种情况既有出场纪录又有订单信息
                    //这两种正常情况:
                    // 如果车型不是临时车，在订单那里会识别成免费，在这里就要识别成正常自动
                    // 如果车型是临时车且费用大于0，在订单那里不会识别免费，在这里要识别成正常收费
                    if (ObjectUtil.isNotEmpty(parkOrderInfo)) {
                        if (ObjectUtil.isNotEmpty(parkOrderInfo.getChargePaid()) && !JddConstant.StringNumber.STRING_ZERO.equals(parkOrderInfo.getChargePaid())) {
                            //正常放行，收费
                            hand.setNormalFeeOut(hand.getNormalFeeOut() + 1);
                        }
                    } else if (!parkCarOutLog.isFreeAllCoupon()) {
                        hand.setNormalNoFeeOut(hand.getNormalNoFeeOut() + 1);
                    }
                } else {
                    //不是临时车非全免券就算自动放行
                    //正常放行，自动
                    if (!parkCarOutLog.isFreeAllCoupon()) {
                        hand.setNormalNoFeeOut(hand.getNormalNoFeeOut() + 1);
                    }
                }
            }
        }
        //金额数量，这里还可以判断免费数
        if (ObjectUtil.isNotEmpty(parkOrderInfo)) {
            //交易成功的
            if (JddConstant.OrderConstant.ORDER_PAY_STATE_SUCCESS.equals(parkOrderInfo.getOrderStatus())) {
                if (ObjectUtil.isNotEmpty(parkOrderInfo.getPayType())) {
                    BigDecimal receivable = BigDecimal.ZERO;
                    BigDecimal paid = BigDecimal.ZERO;
                    BigDecimal free = BigDecimal.ZERO;
                    //应收
                    if (ObjectUtil.isNotEmpty(parkOrderInfo.getChargeDue())) {
                        receivable = new BigDecimal(parkOrderInfo.getChargeDue()).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
                    }
                    //实收
                    if (ObjectUtil.isNotEmpty(parkOrderInfo.getChargePaid())) {
                        paid = new BigDecimal(parkOrderInfo.getChargePaid()).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
                    }
                    //免费
                    if (ObjectUtil.isNotEmpty(parkOrderInfo.getCouponAmt())) {
                        free = new BigDecimal(parkOrderInfo.getCouponAmt()).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
                        //使用优惠卷时计算实际收费
                        if (ObjectUtil.isNotEmpty(parkOrderInfo.getChargePaid())) {
                            paid = receivable.subtract(free);
                        }
                    }
                    if (JddConstant.StringNumber.STRING_SIX.equals(parkOrderInfo.getPayType())) {
                        //线下收费：现金
                        hand.setManualReceivable(hand.getManualReceivable().add(receivable));
                        hand.setManualPaid(hand.getManualPaid().add(paid));
                        hand.setManualFree(hand.getManualFree().add(free));
                    } else {
                        //线上收费：扫码和储值
                        hand.setOnlineReceivable(hand.getOnlineReceivable().add(receivable));
                        hand.setOnlinePaid(hand.getOnlinePaid().add(paid));
                        hand.setOnlineFree(hand.getOnlineFree().add(free));
                    }
                }
                //免费数
                //付款金额或者总金额为0，应对没用卷时，手动环境
                if (JddConstant.StringNumber.STRING_ZERO.equals(parkOrderInfo.getChargePaid()) || JddConstant.StringNumber.STRING_ZERO.equals(parkOrderInfo.getChargeTotalFee())) {
                    //如果有场内码则不为免费
                    if (JddConstant.OrderConstant.ORDER_PAY_STATE_SUCCESS.equals(parkOrderInfo.getOrderStatusDesc())) {
                        hand.setNormalFeeOut(hand.getNormalFeeOut() + 1);
                    } else {
                        //异常数为0免费数加1
                        if (JddConstant.IntNumber.INT_ZERO == abnormal) {
                            hand.setFreeOut(hand.getFreeOut() + 1);
                        }
                    }
                }
                //使用优惠卷时，应对全免卷时
                if (ObjectUtil.isNotEmpty(parkOrderInfo.getCouponAmt()) && parkOrderInfo.getCouponAmt().equals(parkOrderInfo.getChargePaid())) {
                    //异常数为0正常自动放行数加1
                    if (JddConstant.IntNumber.INT_ZERO == abnormal) {
                        hand.setNormalNoFeeOut(hand.getNormalNoFeeOut() + 1);
                    }
                }
            }
        }
        hand.setSynchronousStatus("N");
        //更新交接班
        boolean flag = updateById(hand);
        if (flag) {
            log.info("---------交接班更新成功--------");
            return hand;
        }
        return hand;
    }

    @Override
    public synchronized ParkHandOverLog updateHandOver(ParkOrderInfo parkOrderInfo, String workUser, int abnormal) {
        List<ParkWorkStation> stations = parkWorkStationMapper.getWorkStation(workUser);
        if (ObjectUtil.isEmpty(stations)) {
            log.error("-----------更新交接班时未找到工作站！！！--------");
            return null;
        }
        ParkHandOverLog hand = parkHandOverLogMapper.getHand(workUser);
        if (ObjectUtil.isEmpty(hand)) {
            log.error("----------------交接班数据统计异常，未找到交接班记录！！！------------");
            return null;
        }
        hand.setAllOutNum(hand.getAllOutNum() + 1);
        if (JddConstant.IntNumber.INT_ONE == abnormal) {
            if (JddConstant.OrderConstant.ORDER_PAY_STATE_SUCCESS.equals(parkOrderInfo.getOrderStatus())) {
                //缴费放行
                hand.setNormalFeeOut(hand.getNormalFeeOut() + 1);
            } else {
                hand.setAbnormalOut(hand.getAbnormalOut() + 1);
            }
        } else {
            if (JddConstant.OrderConstant.ORDER_PAY_STATE_SUCCESS.equals(parkOrderInfo.getOrderStatus())) {
                // 正常支付订单，缴费放行
                hand.setNormalFeeOut(hand.getNormalFeeOut() + 1);
            } else {
                // 未支付订单，免费放行
                hand.setFreeOut(hand.getFreeOut() + 1);
            }
        }
        BigDecimal chargeDue = MathUtil.divide(parkOrderInfo.getChargeDue(), "100");
        BigDecimal chargePaid = MathUtil.divide(parkOrderInfo.getChargePaid(), "100");
        hand.setManualReceivable(MathUtil.add(hand.getManualReceivable(), chargeDue));
        hand.setManualPaid(MathUtil.add(hand.getManualPaid(), chargePaid));
        hand.setManualFree(MathUtil.add(hand.getManualFree(), MathUtil.subtract(chargeDue, chargePaid)));
        //更新交接班
        hand.setSynchronousStatus("N");
        boolean flag = updateById(hand);
        if (flag) {
            log.info("---------交接班更新成功--------");
            return hand;
        }
        return null;
    }

    @Override
    public List<ParkHandOverLog> selectSynchronousStatus() {
        return parkHandOverLogMapper.selectSynchronousStatus();
    }

    /*** 功能描述: 将工作站操作员名字改成当前登录的
     * @param logoutName 交班前工作站的账户
     * @param sysUser 交班后工作站得账户
     * @Author: lcy
     * @Date: 2021/11/6
     */
    public ParkWorkStation changeWorkStation(String logoutName, SysUser sysUser) {
        QueryWrapper<ParkWorkStation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operation_name", logoutName);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last(" limit 1");
        ParkWorkStation workStation = parkWorkStationService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(workStation)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //工作站更新成当前登录人的
            workStation.setOperationName(sysUser.getUsername());
            workStation.setRealName(sysUser.getRealname());
            workStation.setUpdateTime(new Date());
            workStation.setUpTime(sdf.format(new Date()));
            workStation.setUsedUserId(sysUser.getId());
            if (parkWorkStationService.updateById(workStation)) {
                return workStation;
            }
        }
        return null;
    }

    /*** 功能描述: 输入交班人账户，将登陆人账户设置到接班人账户
     * @param logoutName 交班人账户
     * @param username 接班人账户
     * @param remark 备注
     * @Author: lcy
     * @Date: 2021/11/6
     */
    public boolean changeHandOver(String logoutName, String username, String remark) {
        QueryWrapper<ParkHandOverLog> wrapper = new QueryWrapper<>();
        //通过交班人名字倒序查找一个，就是当前交班人最新的一条数据
        wrapper.eq("operation_name", logoutName);
        wrapper.isNull("hand_over_name");
        wrapper.orderByDesc("up_time");
        wrapper.last("limit 1");
        ParkHandOverLog handOverLog = this.getOne(wrapper);
        if (ObjectUtil.isEmpty(handOverLog)) {
            log.error("----------未查询到交接班记录------------------");
            return false;
        }
        //当前登陆人账户
        handOverLog.setSynchronousStatus("N");
        handOverLog.setHandOverName(username);
        handOverLog.setOverTime(new Date());
        handOverLog.setRemark(remark);
        return updateById(handOverLog);
    }

    public void logout() {
        //用户退出逻辑
        Subject subject = SecurityUtils.getSubject();
        LoginUser sysUser = (LoginUser) subject.getPrincipal();
        log.info(" 用户名:  " + sysUser.getRealname() + ",退出成功！ ");
        subject.logout();
        //清空用户Token缓存
        cacheUtil.delete(CommonConstant.TOKEN_CACHE_NAME, CommonConstant.PREFIX_USER_TOKEN + sysUser.getId());
        //清空用户权限缓存：权限Perms和角色集合
        cacheUtil.delete(CommonConstant.TOKEN_CACHE_NAME, CommonConstant.LOGIN_USER_CACHERULES_ROLE + sysUser.getUsername());
        cacheUtil.delete(CommonConstant.TOKEN_CACHE_NAME, CommonConstant.LOGIN_USER_CACHERULES_PERMISSION + sysUser.getUsername());

    }

    /**
     * 用户信息
     *
     * @param sysUser
     * @param result
     * @return
     */
    private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
        String syspassword = sysUser.getPassword();
        String username = sysUser.getUsername();
        String userId = sysUser.getId();
        String parkCode = "";
        String parkName = "";

        // 获取车场信息
        List<ParkInfo> list = parkInfoService.list();
        if (!CollectionUtils.isEmpty(list)) {
            parkCode = list.get(0).getParkCode();
            parkName = list.get(0).getParkName();
        }
        // 生成token
        String token = JwtUtil.sign(username, userId, parkCode, parkName, syspassword);
//		redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + userId, token, JwtUtil.EXPIRE_TIME / 1000);
        cacheUtil.put(CommonConstant.TOKEN_CACHE_NAME, CommonConstant.PREFIX_USER_TOKEN + userId, token);
        // 获取用户部门信息
        JSONObject obj = new JSONObject();
        obj.put("token", token);
        obj.put("userInfo", sysUser);
        result.setResult(obj);
        result.success("登录成功");
        return result;
    }

    /**
     * 根据车辆编号、车牌类型匹配停车场的车辆类型Id
     *
     * @param type 车牌类型
     * @return java.lang.String
     * @version 1.0
     */
    private String getCarType(int type) {
        String result = null;
        List<ConfigCarType> list = carTypeService.getCarTypeList();
        for (ConfigCarType carType : list) {
            String carTypeName = carType.getCarTypeName();
            List<String> typeNo = Arrays.asList(carType.getCarTypeNo().split(","));
            if (typeNo.contains(type + "")) {
                result = carTypeName;
                return result;
            }
        }
        return result;
    }
}
